﻿using MiniFox.Components;
using MiniFox.Platform.File;
using MiniFox.Platform.File.Models;
using MiniFox.Platform.File.Services;
using MiniFox.Platform.File.Services.Impls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Platform.Core.Services
{
    [AutoResolve]
    public class GeneraicFileService : DefaultFileService, IRandomFileLoader
    {
        public GeneraicFileService(string groupName) : base(groupName)
        {
        }

        public FileData RandomLoad(string path)
        {
            var fileDatas = this.GetFiles(path);
            if (fileDatas == null || fileDatas.Length <= 0)
                return null;
            int index = new Random().Next(0, fileDatas.Length);
            FileData fileData = fileDatas[index];
            this.OnLoad(fileData);
            return fileData;
        }

        public async Task<FileData> RandomLoadAsync(string path)
        {
            var fileDatas = this.GetFiles(path);
            if (fileDatas == null || fileDatas.Length <= 0)
                return null;
            int index = new Random().Next(0, fileDatas.Length);
            FileData fileData = fileDatas[index];
            await this.OnLoadAsync(fileData);
            return fileData;
        }
    }
}
